<%--
    Document   : artist
    Created on : Mar 26, 2013, 2:30:41 PM
    Author     : tiwtiger
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<link href="css/block.css" type="text/css" rel="stylesheet"/>
<link href="css/homePage.css" type="text/css" rel="stylesheet"/>

<script type="text/javascript" src="js/vietnamese.js"></script>
<script type="text/javascript" src="js/commonjs.js"></script>
<script type="text/javascript">
    var artistDoc;
    var songDoc;
    var input;

    function showArtist(){
        var txt="";
        try
        {
            //reset();
            //input = document.getElementById("search_value").value;
            input = getURLParameters("ArtistID");
            var str = input.trim().toUpperCase().VNize().removeAccent();

            if(str!=""){
                var path = "${pageContext.request.contextPath}/xml/Artists.xml";
                artistDoc = loadXMLDoc(path);
                var artists = artistDoc.getElementsByTagName("Artist");
                var rs = [];

                for(var i=0;i<artists.length;i++){
                    var artistID = artists[i].getAttribute("ArtistID");

                    if(artistID == str){
                        var artistName = artists[i].getElementsByTagName("ArtistName")[0].firstChild.nodeValue;
                        var biology ="Đang cập nhật";
                        if(artists[i].getElementsByTagName("Biology")[0].firstChild!=null){
                            biology = artists[i].getElementsByTagName("Biology")[0].firstChild.nodeValue;
                        }
                        var artistImageSRC = "";
                        if(artists[i].getElementsByTagName("ArtistImageSRC")[0].firstChild!=null){
                            artistImageSRC = artists[i].getElementsByTagName("ArtistImageSRC")[0].firstChild.nodeValue;
                        }
                        var albums = artists[i].getElementsByTagName("Album");
                        var album = [];
                        for(var j=0;j<albums.length;j++){
                            var albumID = albums[j].getAttribute("AlbumID");
                            var albumTitle = albums[j].getElementsByTagName("AlbumTitle")[0].firstChild.nodeValue;
                            album.push({
                                albumID:albumID,
                                albumTitle:albumTitle
                            });
                        }

                        rs.push({
                            artistID:artistID,
                            artistName:artistName,
                            biology:biology,
                            artistImageSRC:artistImageSRC,
                            album:album
                        });
                        break;
                    }// end comparison
                }// end for
                var output="<ul class='list_content'>"
                    +"<div class='list_content_header'>THÔNG TIN NGHỆ SĨ</div>"
                    +"<table><tr><td width='100px'></td><td>";
                if(rs[0].artistImageSRC==""){
                    output+="<img src='${pageContext.request.contextPath}/img/ArtistCoverImage.jpg' "
                        +"witdh='200px' height='200px' /></td></tr>";
                }else{
                    output+="<img src='${pageContext.request.contextPath}/db/"+rs[0].artistName.removeDotCharacter()+"/"
                        +rs[0].artistImageSRC+"' witdh='200px' height='200px' /></td></tr>";
                }
                    
                output+="<tr><td>Nghệ sĩ</td><td>"
                    +"<p class='artist_name'>"
                    +rs[0].artistName
                    +"</p></td></tr><tr><td>Lược sử</td><td>"+rs[0].biology+"</td></tr>"
                    +"<tr><td>Albums</td><td class='artist_name'>";
                var albumList = rs[0].album;
                for(var i=0;i<albumList.length;i++){
                    output+="<a href='musicplayer.jsp?id="+albumList[i].albumID+"&type=2' >"+albumList[i].albumTitle+"</a><br/>";
                }
                output+="</td></tr></table></ul>";
                document.getElementById("output_info").innerHTML=output;
                var artistID = rs[0].artistID;
                showSongs(artistID);
            }
        }
        catch(err)
        {
            txt="There was an error on this page.\n\n";
            txt+="Error description: (showArtist) " + err.message + "\n\n";
            txt+="Click OK to continue.\n\n";
            alert(txt);
        }
    }

    function showSongs(ID){
        var txt="";
        try{
            var path = "${pageContext.request.contextPath}/xml/Songs.xml";
            songDoc = loadXMLDoc(path);
            var songs = songDoc.getElementsByTagName("Song");

            var song = [];
            var mv = [];
            for(var i=0;i<songs.length;i++){
                var artists = songs[i].getElementsByTagName("Artist");
                for(var j=0;j<artists.length;j++){
                    var artistID = artists[j].getElementsByTagName("ArtistID")[0].firstChild.nodeValue;
                    if(artistID==ID){
                        var songID = songs[i].getElementsByTagName("SongID")[0].firstChild.nodeValue;
                        var songTitle = songs[i].getElementsByTagName("SongTitle")[0].firstChild.nodeValue;
                        var views = songs[i].getElementsByTagName("SongViews")[0].firstChild.nodeValue;
                        var likeNumber = songs[i].getElementsByTagName("LikeNumber")[0].firstChild.nodeValue;
                        var isMVs = songs[i].getElementsByTagName("isMV")[0].firstChild.nodeValue;
                        var songSRC = songs[i].getElementsByTagName("SongSRC")[0].firstChild.nodeValue;

                        if(isMVs == "false"){
                            song.push({
                                songID:songID,
                                songTitle:songTitle,
                                views:views,
                                likeNumber:likeNumber,
                                isMVs:isMVs,
                                songSRC:songSRC
                            });
                        }else{
                            mv.push({
                                songID:songID,
                                songTitle:songTitle,
                                views:views,
                                likeNumber:likeNumber,
                                isMVs:isMVs,
                                songSRC:songSRC
                            });
                        }
                        break;
                    }
                }
            }
            //                    bubbleSort(song);
            //                    bubbleSort(mv);
            sortByViews(song);
            sortByViews(mv);
            var outputSong="<ul class='list_content'>"
                +"<div class='list_content_header'>BÀI HÁT NỔI BẬT</div>";
            for (var i=0;i<song.length;i++){
                outputSong += "<li class='song'>"
                    +"<p class='song_title'>"
                    +"<input type='button' value='Nghe thử' onclick='preview("
                    +song[i].songSRC+")'/>"
                    +"<a href='musicplayer.jsp?id="+song[i].songID+"&type=1'>"+song[i].songTitle+"</a>"
                    +"</p>"
                    +"<p class='song_status'>"
                    +song[i].likeNumber+" like"
                    +"</p>"
                    +"<p class='song_status'>"
                    +song[i].views+" views"
                    +"</p></li>";
            }// end for
            outputSong += "</ul>";
            document.getElementById("output_song").innerHTML=outputSong;

            var outputMV="<ul class='list_content'>"
                +"<div class='list_content_header'>MUSIC VIDEO NỔI BẬT</div>";
            for (var i=0;i<mv.length;i++){
                outputMV += "<li class='song'>"
                    +"<p class='song_title'>"
                    +"<input type='button' value='Preview' onclick='preview("
                    +mv[i].songSRC+")'/>"
                    +"<a href='musicplayer.jsp?id="+mv[i].songID+"&type=4'>"+mv[i].songTitle+"</a>"
                    +"</p>"
                    +"<p class='song_status'>"
                    +mv[i].likeNumber+" like"
                    +"</p>"
                    +"<p class='song_status'>"
                    +mv[i].views+" views"
                    +"</p></li>";
            }// end for
            outputMV += "</ul>";
            document.getElementById("output_mv").innerHTML=outputMV;
        }catch(err){
            txt="There was an error on this page.\n\n";
            txt+="Error description: (showSongs) " + err.message + "\n\n";
            txt+="Click OK to continue.\n\n";
            alert(txt);
        }
    }

    function preview(){
        alert(songSRC);
    }

    function reset(){
        var txt = "";
        try
        {
            var searchBox = document.getElementById("search_result");
            searchBox.innerHTML = "";
        }
        catch(err)
        {
            txt="There was an error on this page.\n\n";
            txt+="Error description: (reset) " + err.message + "\n\n";
            txt+="Click OK to continue.\n\n";
            alert(txt);
        }
    }
</script>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Artist Page</title>

    </head>
    <body onload="showArtist()">


        <!-- Include TOP  -->
        <%@include file="masterpage_top.jsp" %>

        <!--Artist Name: <input id="search_value" type="text" name="txtArtistName" value="" />
        <input type="submit" value="Search" name="txtSearch" onclick="showArtist()"/>-->
        <div id="middle">
            <div id="middle_content">
                <div id="output_info"></div>
                <div id="output_song"></div>
                <div id="output_mv"></div>
            </div>
        </div>

        <!--<script type="text/javascript" >
            window.onload=showArtist();
        </script>-->

        <!-- Include BOTTOM  -->
        <%@include file="masterpage_bottom.jsp" %>
    </body>
</html>
